// https://vuex.vuejs.org/zh-cn/intro.html
// make sure to call Vue.use(Vuex) if using a module system
import Vue from 'vue'
import Vuex from 'vuex'
import * as types from './types'

Vue.use(Vuex)

const mutations = {
  [types.CLIENT_TYPE]: (state, type) => {
    state.clientType = type
  },
  [types.SYS_INFO]: (state, sys) => {
    state.sys = sys
  },
  [types.USER_INFO]: (state, user) => {
    state.user = user
  },
  [types.WEBVIEW_LOAD_URL]: (state, url) => {
    state.webviewURL = url
  },
}

const getters = {
  // 用户
  user: state => state.user,
  // Authorization token
  token: state => {
    return state.user && state.user.accessToken
  },
  userId: state => {
    return state.user && state.user.userId
  },
  // 屏幕宽度
  screenWidth: state => state.sys && state.sys.screenWidth,
  // 屏幕高度
  screenHeight: state => state.sys && state.sys.screenHeight,
  // 是否是iOS系统
  isIOS: state => state.sys && state.sys.platform === 'ios',
  // 状态栏高度
  statusBarHeight: state => state.sys && state.sys.statusBarHeight,
  // 是否是小程序
  isMini: state => state.clientType && state.clientType.indexOf('weixin') !== -1,
}

const store = new Vuex.Store({
  state: {
    clientType: '',
    user: null, // 登录用户信息
    sys: null, // 系统相关信息
    webviewURL: '', // 加载webview
  },
  mutations,
  getters,
})

export default store
